#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
 struct TreeNode {
     int val;
     struct TreeNode *left;
      struct TreeNode *right;
 };
 typedef struct TreeNode TreeNode;
 int count1(TreeNode* node)
 {
     return node == NULL ? 0 : count1(node->left) + count1(node->right) + 1;
 }
 void _preorderTraversal(int* a, TreeNode* root, int* i)
 {
     if (root == NULL)
     {
         return;
     }
     a[(*i)++] = root->val;
     _preorderTraversal(a, root->left, i);
     _preorderTraversal(a, root->right, i);
 }
int* preorderTraversal(struct TreeNode* root, int* returnSize) 
{
    int count = count1(root);
    int* ret = (int*)malloc(sizeof(int) * count);
    *returnSize = count;
    int i = 0;
    _preorderTraversal(ret, root, &i);
    return ret;
}